//
//  RW_Players.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_Players.h"
#import "RW_AVAudioPlayerTools.h"
#import "NSFileManager+WLCache.h"
@interface RW_Players ()<RW_AVAudioPlayerToolsDelegate>
@property (nonatomic,strong) RW_AVAudioPlayerTools *wlclass_player_tools;
@end
@implementation RW_Players
- (instancetype)init
{
    self = [super init];
    if (self) {
        _play_runLoop_type = Player_RunLoop_Type_Default;
        _play_status_type = Player_Status_Type_Default;
    }
    return self;
}
#pragma mark -- 初始化数据！必须
-(void)setSongList:(NSArray<RW_SongModel *> *)songList{
    _songList = songList;
    [self change_play_index:0];
}
#pragma mark -- 功能性方法或者属性
/** 修改当前播放位置 */
-(void)change_play_second:(NSInteger)second{
    [self.wlclass_player_tools setCurrentTime:second];
}
/** 修改当前播放的声音大小 0-100 */
-(void)change_play_volume:(NSInteger)volume{
    self.wlclass_player_tools.volume = volume;
}
/** 修改当前播放的下标 */
-(void)change_play_index:(NSInteger)index{
    [self changePlayIndex:index isPlaying:false];
}
/** 修改播放循环模式 */
-(void)change_runLoop_type:(Player_RunLoop_Type)play_runLoop_type{
    self.play_runLoop_type = play_runLoop_type;
}
/** 上一首 */
-(void)lessPlayIndex{
    if ((self.play_index - 1) >= 0){
        [self changePlayIndex:(self.play_index - 1) isPlaying:true];
    } else {
        [self changePlayIndex:(self.songList.count - 1) isPlaying:true];
    }
}
/** 下一首 */
-(void)nextPlayIndex{
    if (self.songList.count > (self.play_index + 1)){
        [self changePlayIndex:self.play_index + 1 isPlaying:true];
    } else {
        [self changePlayIndex:0 isPlaying:true];
    }
}
/** 开始播放 */
-(void)beginPlay{
    [self changePlayIndex:self.play_index isPlaying:true];
    [self.wlclass_player_tools playAudio];
}
/** 停止播放 */
-(void)stopPlay{
    [self.wlclass_player_tools stopAudio];
}
/** 暂停播放 */
-(void)pausePlay{
    [self.wlclass_player_tools pauseAudio];
}

#pragma mark -- 核心方法
/**
 修改当前播放下标
 @index 播放下标
 @isPlaying 切换后是否直接播放
 */
-(void)changePlayIndex:(NSInteger)index isPlaying:(BOOL)isPlaying{
    //如果播放下表没变则带遍只是正常的暂停开始
    if (self.play_index == index && self.wlclass_player_tools.audioPlayer){
        [self.wlclass_player_tools playAudio];
        return;
    }
    //先停止播放
    [self.wlclass_player_tools stopAudio];
    //切换歌曲
    RW_SongModel *songModel = [self.songList objectAtIndex:index];
    //为空则跳过
    if (!songModel){return;}
    //有则修改当前播放下标和对象
    self.play_index = index;
    self.play_song = songModel;
    
    //状态默认为下载中
    self.play_status_type = Player_Status_Type_Download;
    [self.play_song downloadAudioProgress:^(float progress) {
        //有进度则代表正在下载
        self.play_download_progress = progress;
    } success:^(NSString * _Nonnull filePath) {
        [self.wlclass_player_tools setAudioPath:filePath];
        if (isPlaying){
            [self.wlclass_player_tools playAudio];
        }
        //完成代表下载完毕，或者缓存读取完毕
        self.play_status_type = Player_Status_Type_InitData;
    } fail:^(NSError * _Nonnull error) {
        //完成代表下载失败或者读取失败
        self.play_status_type = Player_Status_Type_Error;
    }];
}

#pragma mark -- RW_AVAudioPlayerToolsDelegate 代理
/** 播放器音频资源初始化 */
-(void)initPlayCurrentTime:(NSInteger)currentTime allCurrentTime:(NSInteger)allCurrentTime{
    self.play_second = currentTime;
    self.play_all_second = allCurrentTime;
}
/** 播放器初始化错误 */
-(void)initAudioError:(NSError *)error{
    NSLog(@"播放器初始化错误:%@",error);
    self.play_status_type = Player_Status_Type_Error; 
}
/** 播放进度回调 */
-(void)currentTime:(NSInteger)currentTime allCurrentTime:(NSInteger)allCurrentTime{
    self.play_second = currentTime;
}
/** 开始播放 */
-(void)playBegin{
    self.play_status_type = Player_Status_Type_Playing;
}
/** 暂停播放 */
-(void)playPause{
    self.play_status_type = Player_Status_Type_Stoping;
}
/** 停止播放 */
-(void)playStop{
    self.play_status_type = Player_Status_Type_Stoping;
}
/** 播放结束回调 */
-(void)playEnd{
    self.play_status_type = Player_Status_Type_Default;
    //当前播放列表中只有一个曲目的时候，默认循环播放
    if (self.songList.count <= 1){
        [self changePlayIndex:0 isPlaying:true];
        return;
    }
    //当前播放列表中的数量大于一个的时候，按照播放循环方式进行播放
    switch (self.play_runLoop_type) {
        case Player_RunLoop_Type_Default:{//默认播放模式 列表循环
            [self nextPlayIndex];
        }
            break;
        case Player_RunLoop_Type_Circulation:{//单曲循环
            [self changePlayIndex:self.play_index isPlaying:true];
        }
            break;
        case Player_RunLoop_Type_Random:{//随机播放
            [self changePlayIndex:(arc4random() % self.songList.count) isPlaying:true];
        }
            break;
        default:
            break;
    }
}
/** 播放出错 */
-(void)playError{
    NSLog(@"播放出错");
}

#pragma mark -- 懒加载
-(RW_AVAudioPlayerTools *)wlclass_player_tools{
    if (!_wlclass_player_tools){
        _wlclass_player_tools = [[RW_AVAudioPlayerTools alloc]init];
        _wlclass_player_tools.delegate = self;
    }
    return _wlclass_player_tools;
}

#pragma mark -- 释放当前播放器属性
-(void)releaseVM{
    [self stopPlay];
    [_wlclass_player_tools releaseVM];
    _songList = nil;
    _play_song = nil;
    _wlclass_player_tools = nil;
}
@end
